package com.golddaymicromall.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.bean.PanicBuyingInfo;
import com.golddaymicromall.img.ImageLoad;
import com.golddaymicromall.model.HttpModel;
import com.golddaymicromall.rxbus.RxBus;

import java.util.List;

/**
 * Created by dawd on 2016/3/11.
 */
public class PanicBuyingAdapter extends BaseAdapter {
    private List<PanicBuyingInfo.DataBean> list;
    private Context ctx;
    private ImageLoad load;
    private int selectID;
    LayoutInflater layoutinflater;

    public PanicBuyingAdapter(List<PanicBuyingInfo.DataBean> list, Context ctx) {
        this.list = list;
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // 自定义的选中方法
    public void setSelectID(int position) {
        selectID = position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder hold;
        View view = convertView;
        if (view == null) {
            hold = new ViewHolder();
            layoutinflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            view = layoutinflater.inflate(R.layout.item_panicbuyinglistview, null);
            hold.goodsImg = (ImageView) view.findViewById(R.id.PanicBuyingItem_Img);
            hold.priceTv = (TextView) view.findViewById(R.id.PanicBuyingItem_PriceTv);
            hold.selectImg = (ImageView) view.findViewById(R.id.PanicBuying_SelectImg);
            hold.titleTv = (TextView) view.findViewById(R.id.PanicBuyingItem_TitleTv);
            hold.PanicBuying_jia = (TextView) view.findViewById(R.id.PanicBuying_jia);
            hold.PanicBuying_jian = (TextView) view.findViewById(R.id.PanicBuying_jian);
            hold.PanicBuying_count = (EditText) view.findViewById(R.id.PanicBuying_count);
            view.setTag(hold);

        } else {
            hold = (ViewHolder) view.getTag();

        }
        hold.selectImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View convertView) {
                PanicBuyingInfo.DataBean info = list.get(position);
                info.flag = !info.flag;
                list.set(position, info);
                RxBus.get().post("rxbus", new String("1"));
                notifyDataSetChanged();
            }
        });
        //这个逻辑放在这里能保证选中后被勾选的条目不错位
        if (list.get(position).flag) {
            hold.selectImg.setImageResource(R.drawable.cart_selent_highlighted);
        } else {
            hold.selectImg.setImageResource(R.drawable.cart_selent_nromal);
        }
        Glide.with(ctx)
                .load(new HttpModel().IMGURL + list.get(position).getPaniccount())
                .into(hold.goodsImg);
        hold.priceTv.setText("￥" + list.get(position).getPrice());
        hold.titleTv.setText(list.get(position).getProductname());
//        procount库存
        hold.PanicBuying_count.setText(list.get(position).getCount() == 0 ? "0" : String.valueOf(list.get(position).getCount()));
        hold.PanicBuying_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText inputCount = new EditText(ctx);
                inputCount.setInputType(InputType.TYPE_CLASS_NUMBER);
                inputCount.setGravity(Gravity.CENTER);
                inputCount.setBackgroundResource(R.drawable.jshop_searchbar_bg);
                AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
                builder.setTitle("请输入数量").setIcon(android.R.drawable.ic_dialog_info).setView(inputCount)
                        .setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        String number = inputCount.getText().toString();
                        if (!number.equals("")) {
                            hold.PanicBuying_count.setText(number);
                            list.get(position).setCount(Integer.parseInt(number));
                            RxBus.get().post("rxbus", new String("1"));
                            notifyDataSetChanged();
                        }

                    }
                });
                builder.show();


            }
        });
        hold.PanicBuying_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(hold.PanicBuying_count.getText().toString()) == 0) {
                    hold.PanicBuying_jian.setClickable(false);
                    return;
                }
                int p = Integer.parseInt(hold.PanicBuying_count.getText().toString());

                p--;
                hold.PanicBuying_count.setText(String.valueOf(p));
                list.get(position).setCount(p);
                RxBus.get().post("rxbus", new String("1"));
                notifyDataSetChanged();
            }
        });
        hold.PanicBuying_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(hold.PanicBuying_count.getText().toString()) > 0) {
                    hold.PanicBuying_jian.setClickable(true);
                }
                if (Integer.parseInt(hold.PanicBuying_count.getText().toString()) > list.get(position).getProcount()) {
                    hold.PanicBuying_jia.setClickable(false);
                }
                int p = Integer.parseInt(hold.PanicBuying_count.getText().toString());

                p++;
                hold.PanicBuying_count.setText(String.valueOf(p));
                list.get(position).setCount(p);
                RxBus.get().post("rxbus", new String("1"));
                notifyDataSetChanged();
            }
        });
        return view;
    }


    public static class ViewHolder {
        ImageView selectImg;
        ImageView goodsImg;
        TextView titleTv;
        TextView priceTv;
        TextView PanicBuying_jian;
        TextView PanicBuying_jia;
        EditText PanicBuying_count;
    }
}
